﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Tracy.CoreLib.Data;
using Tracy.CoreLib;
using System.Collections.Specialized;
using System.Threading;
using CoreLib.Message;
namespace Tracy.Engine
{
    /// <summary>
    /// TracyEngine，主要逻辑在这里实现
    /// </summary>
    public partial class TracyEngine
    {
        private MessageWrapper _wrapper;

        public MessageWrapper MessageGenerator
        {
            get { if (_wrapper == null)_wrapper = new MessageWrapper(this); return _wrapper; }
        }

        /// <summary>
        /// 消息包装器，用于对UI的接口
        /// </summary>
        public class MessageWrapper
        {
            private TracyEngine _engine;
            public TracyEngine Engine
            {
                get { return _engine; }
            }
            public MessageWrapper(TracyEngine engine)
            {
                _engine = engine;
            }

            public void SendToDownload(object sender, int id)
            {
                
                DownloadMessage msg = new DownloadMessage(sender, Engine.Bus.ResourceProvider.GetResourceById(id));
                Engine.EnqueueMessage(msg);
            }

            public void RemoveItem(object sender, IManagedXmlModelItem item)
            {
                Engine.EnqueueMessage(new RemoveItemMessage(sender, item));
            }

            public void AddItem(object sender, IManagedXmlModelItem item)
            {
                Engine.EnqueueMessage(new AddItemMessage(sender, item));
            }

            public void ReplaceItem(object sender, IManagedXmlModelItem newItem, IManagedXmlModelItem oldItem)
            {
                Engine.EnqueueMessage(new ReplaceItemMessage(sender, newItem, oldItem));
            }

            public void UpdateItem(object sender, IManagedXmlModelItem item, string key, string value)
            {
                Engine.EnqueueMessage(new UpdateItemMessage(sender, item, key, value));
            }

            public void UpdateStatusById(object sender, int id, string status)
            {
                UpdateItem(sender, Engine.Bus.ResourceProvider.GetResourceById(id), Constants.CONST_TAG_STATUS, status);
            }

            public void UpdateFeed(object sender)
            {
                Engine.EnqueueMessage(new UpdateFeedMessage(sender));
            }

            public int RetrieveNewResource(object sender)
            {
                int rtn = 0;
                IXmlModelCollection filterResult = null;
                foreach (RssFilter filter in Engine.GetRssFilters())
                {
                    filterResult = Engine.ExecuteFilter(filter);
                    foreach (ResourceItem item in filterResult)
                    {
                        if (!Engine.GetCurrentResources().Contains(item.Key))
                        {
                            Engine.MessageGenerator.AddItem(sender, item);
                            rtn++;
                        }
                    }
                }
                return rtn;
            }

            public void InitEngine(object sender)
            {
                if (!Engine.EngineInitiated)
                {
                    Engine.EnqueueMessage(new InitEngineMessage(sender));
                }
                else
                {
                    throw new Exception("TracyEngine已经初始化了！");
                }
            }

            public void AssignTag(object sender, TracyProperty property, IManagedXmlModelItem source, IManagedXmlModelItem target)
            {
                Engine.EnqueueMessage(new AssignTagMessage(sender, property, source, target));
            }

            public void RemoveTag(object sender, TracyRelationship relationship)
            {
                Engine.EnqueueMessage(new RemoveTagMessage(sender, relationship));
            }

            public void RemoveTag(object sender, IManagedXmlModelItem source, IManagedXmlModelItem target)
            {
                Engine.EnqueueMessage(new RemoveTagMessage(sender, source, target));
            }
        }
    }
}